package cn.gov.msa.controller;

import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;

import cn.gov.msa.model.Questions;
import cn.gov.msa.service.QuestionsService;
import cn.gov.msa.util.GsonHelper;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@RestController
@RequestMapping(value = "/questions", produces = "text/plain;charset=UTF-8")
public class QuestionsController {
	
	ObjectMapper mapper = new ObjectMapper();
	
	@Autowired
	private QuestionsService questionsService;
		
	@RequestMapping(value = "/list")
	public String list(@RequestBody JSONObject params, HttpSession session) throws Exception {	
		
		String result = "";		
		int pageNum = 1;
		int pageSize = 10;
		System.out.println(params);
		if(params.has("pageNum"))
		{
			pageNum = params.getInt("pageNum");
			params.remove("pageNum");
		}
		if(params.has("pageSize"))
		{
			pageSize = params.getInt("pageSize");
			params.remove("pageSize");
		}
		
		Questions questions = GsonHelper.convertEntity(params.toString(), Questions.class);
		
		if("".equals(params.optString("orgCode"))){
			questions.setOrgCode(questions.getSysorg());
		}
		
		List<Questions> questionList = questionsService.selectData(pageNum, pageSize, questions);	
	    //获取分页信息
        PageInfo<Questions> page = new PageInfo<Questions>(questionList);       
		JSONArray ja = JSONArray.fromObject(questionList);
		for(int i=0; i<ja.size(); i++){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String str = sdf.format(questionList.get(i).getPublishDate());
			ja.getJSONObject(i).put("publishDate", str);
		}
		JSONObject jsonResult = new JSONObject();
		jsonResult.put("total", page.getTotal());
		jsonResult.put("rows", ja);				
		result = jsonResult.toString();
		return result;
	}
	
	@RequestMapping(value = "/save")
	public String insert(@RequestBody JSONObject param) throws Exception {
				

		JSONObject jsonResult = new JSONObject();
		
		Questions questions = GsonHelper.convertEntity(param.toString(), Questions.class);
		int result = 0;
		if(questions.getQuestionsId() != null){
			result = questionsService.update(questions);
		} else {
			result = questionsService.insert(questions);
		}
		
		jsonResult.put("result", result);
		if(result > 0){
			jsonResult.put("info", "保存数据成功！");
		} else {
			jsonResult.put("info", "保存数据失败！");
		}
		
		return jsonResult.toString();
	}
	
	@RequestMapping(value = "/update")
	public String update(@RequestBody JSONObject param) throws Exception {	
				

		JSONObject jsonResult = new JSONObject();
		
		System.err.println(param);
		Questions questions =GsonHelper.convertEntity(param.toString(), Questions.class);
		
		int result=questionsService.update(questions);
		
		jsonResult.put("result", result);
		if(result > 0){
			jsonResult.put("info", "更新数据成功！");
		} else {
			jsonResult.put("info", "更新数据失败！");
		}
		return jsonResult.toString();
	}
	
	@RequestMapping(value = "/delete")
	public String delete(Questions question) throws Exception {	
				
		JSONObject jsonResult = new JSONObject();
				
		int result = questionsService.delete(question.getQuestionsId());
		jsonResult.put("result", result);
		if(result > 0){
			jsonResult.put("info", "删除数据成功！");
		} else {
			jsonResult.put("info", "删除数据失败！");
		}
		return jsonResult.toString();
	}
	
	@RequestMapping(value = "/deleteByMsgId")
	public String deleteByMsgId(Questions question) throws Exception {	
		
		JSONObject jsonResult = new JSONObject();
		
		int result = questionsService.deleteByMsgId(question.getMsgId());
		jsonResult.put("result", result);
		if(result > 0){
			jsonResult.put("info", "删除数据成功！");
		} else {
			jsonResult.put("info", "删除数据失败！");
		}
		return jsonResult.toString();
	}
	
}
